layui.use(['laypage', 'layer'], function(){
    // 默认翻页参数
    var datapage = {
        "page": 1,
        "pageNumber": 20,
    }
    $.ajax({
        type: 'POST',
        url: "/signing/getSigning",
        dataType:"json",
        async: false,
        data:{
            page: datapage.page,
            pageNumber: datapage.pageNumber,
            name: '',
            type: '',
            state: '',
            remarks: '',
        },
        success: function (message) {
            datapage.dataCount = message.data.dataCount
        }
    })
    var laypage = layui.laypage
        ,layer = layui.layer;
    //总页数低于页码总数
    laypage.render({
        limit:datapage.pageNumber,
        jump: function(obj, first){
            datalist(obj.curr, obj.limit,'','','','') // 处理列表信息
        },
        layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
        limits:[20,40,60,80,100],
        elem: 'page_all',
        count: datapage.dataCount //数据总数
    });
})

// 获取列表数据
function datalist(page,pageCount,name,type,state,remarks){
    // 清空一下列表
    $("#signing_data").empty();//清空节点
    return $.ajax({
        type: 'POST',
        url: "/signing/getSigning",
        dataType:"json",
        data:{
            page: page,
            pageNumber: pageCount,
            name: name,
            type: type,
            state: state,
            remarks: remarks,
        },
        success: function (message) {
            $("#signing_data").append(analysis(message))
            $("#name").val(name) // 备注
            // $("#type").val(type)// 状态
            $("#state").val(state)// 级别
            $("#remarks").val(remarks) // 名字
            var page = {"page":message.data.page,"pageCount":message.data.pageCount,"pageNumber":message.data.pageNumber}
            return page
        }
    })
}

// 搜索签约
function businessSelect() {
    var name = $("#name").val(); // 用户名
    var type = $("#type").val();
    var state = $("#state").val();
    var remarks = $("#remarks").val();
    if(isEmpty(name) && isEmpty(type) && isEmpty(state) && isEmpty(remarks)){
        layer.msg('请输入搜索数据！');
    }else {
        $.ajax({
            type: 'POST',
            url: "/signing/getSigning",
            dataType:"json",
            data:{
                page:1,
                pageNumber:20,
                name:name,
                type:type,
                state:state,
                remarks:remarks,
            },
            success: function (message) {
                console.log(message)
                // 清空一下节点
                $("#signing_data").empty();
                // 节点下面铺数据
                $("#signing_data").append(analysis(message))
                // 重新铺一下页码
                pageRestructure("page_all",message.data.dataCount,20,name,type,state,remarks)
            }
        })
    }
}


// 解析列表data列表页面
function analysis(message) {
    trlist = []
    for (i = 0;i < message.data.data.length;i++ ){
        var dataInformation = message.data.data[i];
        var remarks = dataInformation.ggSRemarks;
        var serialNumber = i+1
        if(isEmpty(remarks)){
            remarks = ""
        }else if(remarks.length>35){
            remarks = remarks.substring(0,35)+'......'
        }
        // 信息唯一标识符
        ggItemsToken = "'"+dataInformation.ggItemsToken+"'"
        // id
        ggBoId = "'"+dataInformation.ggSId+"'"
        var tr =
            '<tr>'+
            '<td><input type="checkbox" name="ggIId" value="'+dataInformation.ggSId+'"> </td>'+
            '<td> '+serialNumber+'</td>'+
            '<td onclick="getTableTr('+ggItemsToken+')">'+dataInformation.ggSName+'</td>'+
            '<td onclick="getTableTr('+ggItemsToken+')">'+dataInformation.ggSProvince+'/'+dataInformation.ggSCity+'/'+ dataInformation.ggSAreaid +'/'+ dataInformation.ggSDetailed + '</td>'+
            '<td onclick="getTableTr('+ggItemsToken+')">'+ dataInformation.ggSHandle+ '</td>'+
            '<td onclick="getTableTr('+ggItemsToken+')"><button type="button" class="layui-btn layui-btn-warm-phone">'+dataInformation.ggSTelephone+'</button></td>'+
            '<td onclick=""><button type="button" class="layui-btn layui-btn-warml">'+dataInformation.ggSLevel+'</button></td>'+
            '<td onclick=""><button type="button" class="layui-btn layui-btn-warm">'+dataInformation.ggSState+'</button></td>'+
            '<td onclick="getTableTr('+ggItemsToken+')">'+remarks+'</button></td>'+
            '<td><a target="_blank" href="./signing-items.html?token_id='+dataInformation.ggItemsToken+'">查看信息</a>  <a href="#" onclick="items_delect('+ggBoId+')">删除信息</a href="">  <a href="#" onclick="businessOpportunityItemsUpdate('+ggItemsToken+')">修改信息</a><br/> <a href="#" onclick="remarksDetails('+ggItemsToken+')">增加备注</a> <a href="#" onclick="move_Information('+dataInformation.ggSId+',1)">移入信息</a> <a href="#" onclick="move_Information('+dataInformation.ggSId+',2)">移入商机</a></td>'+
            '</tr>'
        trlist.push(tr)
    }
    return trlist
}


// 增加商机备注
function remarksDetails(token_id) {
    // console.log(id)
    //默认prompt
    layer.prompt({
        formType: 2,
        title: '请输入备注内容',
        area: ['24rem', '15rem'] //自定义文本域宽高
    },function(val, index,elem){
        // console.log(val)

        $.ajax({
            type: 'POST',
            url: "/remarks/addGgRemarks",
            dataType:"json",
            data:{
                count: val,
                token: token_id,
                type:3
            },
            success: function (message) {
                if(message.code==0){
                    layer.msg('增加备注成功！');
                }else {
                    layer.msg('增加备注失败请重试！');
                }

            },error: function (message) {
                layer.msg('增加备注失败请重试！');
            }
        })
        layer.close(index);
        sleep(200)
        datalist(1,20,'','','','');
    });
}

// 增加签约
function addInformAtion() {
    layer.open({
        type: 2,
        area: ["1100px", "600px"],
        fixed: false, //不固定
        maxmin: true,
        title: '增加签约',
        content: 'siging-items-add.html'
    });
}

// 点击显示备注
function getTableTr(id) {
    if(!isEmpty(id)){
        layer.open({
            type: 2,
            area: ['700px', '450px'],
            fixed: false, //不固定
            maxmin: true,
            title: '备注详情',
            closeBtn: 0,
            shadeClose: true,
            skin: 'layui-layer-rim', //加上边框
            content: 'remarks-details.html?id='+id
        });
    }else {
        layer.msg('请选择要备注的数据！');
    }
}

// 修改信息
function businessOpportunityItemsUpdate(ggItemsToken) {
    layer.open({
        type: 2,
        area: ["1100px", "600px"],
        fixed: false, //不固定
        maxmin: true,
        title: '修改商机/线索',
        content: 'siging-items-update.html?ggItemsToken='+ggItemsToken
    });
}

// 删除签约
function items_delect(id) {
    if(isEmpty(id)){
        layer.msg('请选择要删除的数据！');
    }else {
        layer.msg('确认删除？ 删除无法撤销！', {
            time:0,
            btn: ['取消','确定'],
            btnAlign: 'c',
            yes: function(index, layero){
                //按钮【按钮一】的回调
                console.log("取消按钮")
                layer.closeAll()
            }, btn2: function(index, layero){
                //按钮【按钮二】的回调
                //return false 开启该代码可禁止点击该按钮关闭
                console.log("确定按钮")
                $.ajax({
                    type: 'POST',
                    url: "/signing/delectSignings",
                    dataType:"json",
                    data:{
                        id: id
                    },
                    success: function (message) {
                        console.log(message)
                        if(message.code==0){
                            layer.msg('成功删除');
                            sleep(200)
                            datalist(1,20,'','','','');
                        }else {
                            layer.msg('删除商机失败，请稍后重试！');
                        }
                    },error: function (message) {
                        layer.msg('删除商机失败，请稍后重试！');
                    }
                })
                layer.closeAll()

            }
        });
    }
}

//  勾选的按钮id
function getInput() {
    var obj = document.getElementsByName("ggIId");
    var check_arr = [];
    for (var i = 0; i < obj.length; i++) {
        if (obj[i].checked)
            check_arr.push(obj[i].value);
    }
    return check_arr
}

// 批量删除
function items_delect_all() {
    var items_id = getInput();
    if(items_id.length<=0){
        layer.msg('请选择要删除信息的数据！');
    }else {
        $.ajax({
            type: 'POST',
            url: "/signing/delectbuSigningsAll",
            dataType:"json",
            data:{
                //id:items_id[i],
                listid: JSON.stringify( items_id )
            },
            success: function (message) {
                console.log(message)
                if(message.code==0){
                    layer.msg('成功：'+message.data.yes+'个，找不到：'+message.data.not+'个，失败：'+message.data.error+'个');
                    sleep(200)
                    datalist(1,20,'','','','');
                }else {
                    layer.msg('删除商机失败，请稍后重试！');
                }
            },error: function (message) {
                layer.msg('删除商机失败，请稍后重试！');
            }
        })
    }
}

// 批量移入信息、商机
function items_Information_all(type) {
    var items_id = getInput();
    if(items_id.length<=0){
        layer.msg('请选择要移入信息的数据！');
    }else {
        console.log()
        $.ajax({
            type: 'POST',
            url: "/signing/removebuSigningsAll",
            dataType:"json",
            data:{
                //id:items_id[i],
                listid: JSON.stringify( items_id ),
                type: type
            },
            success: function (message) {
                if(message.code==0){
                    layer.msg('成功：'+message.data.yes+'个，找不到：'+message.data.not+'个，失败：'+message.data.error+'个');
                    sleep(200)
                    datalist(1,20,'','','','');
                }else {
                    layer.msg('加入商机失败，请稍后重试！');
                }
            },error: function (message) {
                layer.msg('加入商机失败，请稍后重试！');
            }
        })

        // sleep(100)
        // datalist(1,20,'','','','');
    }
}

// 单个移入商机、信息
function move_Information(itmesid,type) {
    if(isEmpty(itmesid) || isEmpty(type)){
        layer.msg('请选择数据');
    }else {
        console.log(itmesid+"------"+type)
        $.ajax({
            type: 'POST',
            url: "/signing/removeSignings",
            dataType:"json",
            data:{
                //id:items_id[i],
                id: itmesid,
                type: type
            },
            success: function (message) {
                if(message.code==0){
                    layer.msg('移除成功！');
                    sleep(200)
                    datalist(1,20,'','','','');
                }else {
                    layer.msg('加入商机失败，请稍后重试！');
                }
            },error: function (message) {
                layer.msg('加入商机失败，请稍后重试！');
            }
        })
    }
}
